<template>
	<view>
		<search-bar @click="goSearch"></search-bar>
		<!-- 内容区域 -->
		<view class="content-box">
			<!-- 左 -->
			<scroll-view class="left" scroll-y="true" :style="{height:wh+'px'}">
				<view :class="[current === index ?'active': '']" @click="changeCate(item,index)"
					v-for="item,index in cateList" :key="index">{{item.cat_name}}</view>
			</scroll-view>
			<!-- 右 -->
			<scroll-view :scroll-top="scrollTop" class="right" scroll-y="true" :style="{height:wh+'px'}">
				<view v-for="item,index in cateList2" :key="index">
					<!-- 标题 -->
					<view class="title">
						/{{item.cat_name}}/
					</view>
					<!-- 内容 -->
					<view class="content">
						<view class="content-item" v-for="e,i in item.children" :key="i" @click="goList(e)">
							<image :src="e.cat_icon"></image>
							<text>{{e.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cateList: [],
				wh: 0, //设备高度初始值
				cateList2: [],
				current: 0,
				scrollTop: 0
			};
		},
		onLoad() {
			this.getCateList()
			//获取设备的屏幕高度   getSystemInfoSync 异步 回调函数中调用
			const res = uni.getSystemInfoSync()
			// 解决 有搜索框后,下面内容不出来 -50
			this.wh = res.windowHeight - 50
		},
		methods: {
			goSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
			//跳转到商品列表
			goList(item) {
				uni.navigateTo({
					url: '/subpkg/goods-list/goods-list?cat_id=' + item.cat_id
				})
			},
			//切换分类
			changeCate(item, index) {
				//切换高亮样式-active
				this.current = index
				//切换右侧内容
				this.cateList2 = item.children
				//恢复右侧滚动位置
				this.scrollTop = this.scrollTop === 0 ? .1 : 0
			},
			//获取分类列表
			async getCateList() {
				const {
					data: {
						message,
						meta
					}
				} = await uni.$http.get('/api/public/v1/categories')
				//console.log(message);
				if (meta.status !== 200) return uni.$showMsg()
				this.cateList = message
				this.cateList2 = message[0].children
			}
		}
	}
</script>

<style lang="scss">
	.content-box {
		display: flex;

		.left {
			width: 120px;

			view {
				font-size: 12px;
				line-height: 60px;
				text-align: center;
				background-color: #f7f7f7;

				//激活项的样式
				&.active {
					background-color: #fff;
					position: relative;

					//渲染激活项左侧的红色指示线
					&::before {
						position: absolute;
						left: 0;
						top: 50%;
						margin-top: -15px;
						content: '';
						width: 2px;
						height: 30px;
						background-color: #ea4350;
					}
				}
			}
		}

		.right {
			.title {
				font-size: 12px;
				font-weight: bold;
				text-align: center;
				margin: 6px 0;
			}

			.content {
				display: flex;
				flex-wrap: wrap;

				.content-item {
					width: calc(100%/3);
					display: flex;
					flex-direction: column;
					align-items: center;
					font-size: 12px;
					margin-bottom: 6px;

					image {
						width: 60px;
						height: 60px;
					}
				}
			}
		}
	}
</style>
